#include "geometry/wsyring.h"
#include "geometry/wsyface.h"
#include "wsymath/wsymath.h"

void WSYRing::generate() {
    smallerRing->clear();
    largerRing->clear();
    vertices->clear();
    edges->clear();

    GLint *smallerEdge = new GLint[segment];
    GLint *largerEdge = new GLint[segment];

    double singleAngle = 2 * M_PI / segment;
    for (int s = 0; s < segment; s++) {
        double cosValue = cos(s * singleAngle),
               sinValue = sin(s * singleAngle);
        double x_smaller = smallerRadius * cosValue;
        double y_smaller = smallerRadius * sinValue;

        double x_larger = largerRadius * cosValue;
        double y_larger = largerRadius * sinValue;

        GLfloat *smallerVertex = new GLfloat[2];
        GLfloat *largerVertex = new GLfloat[2];

        WSYMath::newVertexValue(x_smaller, y_smaller, smallerVertex);
        WSYMath::newVertexValue(x_larger, y_larger, largerVertex);

        smallerRing->append(smallerVertex);
        largerRing->append(largerVertex);

        int thisIndexOfSmaller = s, nextIndexOfSmaller = fmod(s + 1, segment);
        int thisIndexOfLarger = thisIndexOfSmaller + segment,
            nextIndexOfLarger = nextIndexOfSmaller + segment;

        WSYFace *face1 = new WSYFace(
                    thisIndexOfSmaller, thisIndexOfLarger, nextIndexOfLarger);
        WSYFace *face2 = new WSYFace(
                    nextIndexOfLarger, nextIndexOfSmaller, thisIndexOfSmaller);

        smallerEdge[s] = s;
        largerEdge[s] = segment * 2 - 1 - s;

        faces->append(face1); faces->append(face2);
    }

    for (int sr = 0; sr < smallerRing->count(); sr++) {
        GLfloat *sVertex = smallerRing->at(sr);
        vertices->append(sVertex);
    }

    for (int lr = 0; lr < largerRing->count(); lr++) {
        GLfloat *sVertex = largerRing->at(lr);
        vertices->append(sVertex);
    }

    edges->append(smallerEdge);
    edges->append(largerEdge);

    edgeCounts->append(segment);
    edgeCounts->append(segment);
}
